package com.example.communist_app_api.util;

import org.apache.poi.ss.formula.functions.T;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author love~mlm
 * @version 1.0
 * Create by 2022/10/1 17:42
 */

public class CommonUtils {
    //输入流转字符串
    public static String InputStreamToString(InputStream inputStream){
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        String str = null;
        int length;
        try {
            while ((length = inputStream.read(buffer)) != -1) {
                result.write(buffer, 0, length);
            }
            str = result.toString(StandardCharsets.UTF_8.name());
        }catch (Exception e){
            e.printStackTrace();
        }
        return str;
    }
    //输入流转jsonObject
    public static JSONObject InputStreamToJsonObject(InputStream inputStream){
        BufferedReader bR = new BufferedReader(  new InputStreamReader(inputStream));
        String line = "";
        StringBuilder responseStrBuilder = new StringBuilder();

        try {
            while((line =  bR.readLine()) != null){
                responseStrBuilder.append(line);
            }
            inputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        JSONObject result= new JSONObject(responseStrBuilder.toString());
        return result;
    }
    //map to object
    /**
     * 把source转为target
     * @param source source
     * @param target target
     * @param <T> 返回值类型
     * @return 返回值
     * @throws Exception newInstance可能会抛出的异常
     */
    public static <T> T mapToObj(Map source, Class<T> target) throws Exception {
        Field[] fields = target.getDeclaredFields();
        T o = target.newInstance();
        for (Field field : fields) {
            Object val;
            if ((val = source.get(field.getName())) != null) {
                field.setAccessible(true);
                field.set(o, val);
            }
        }
        return o;
    }

    /**
     * @Title: combineSydwCore
     * @Description: 该方法是用于相同对象不同属性值的合并，如果两个相同对象中同一属性都有值，
     *               那么sourceBean中的值会覆盖tagetBean重点的值
     * @author: WangLongFei
     * @date: 2017年12月26日 下午1:53:19
     * @param sourceBean
     *            被提取的对象bean
     * @param targetBean
     *            用于合并的对象bean
     * @return targetBean 合并后的对象
     * @return: Object
     */
    @SuppressWarnings("unused")
    public  static Object combineSydwCore(Object sourceBean, Object targetBean) {
        Class sourceBeanClass = sourceBean.getClass();
        Class targetBeanClass = targetBean.getClass();

        Field[] sourceFields = sourceBeanClass.getDeclaredFields();
        Field[] targetFields = sourceBeanClass.getDeclaredFields();
        for (int i = 0; i < sourceFields.length; i++) {
            Field sourceField = sourceFields[i];
            Field targetField = targetFields[i];
            sourceField.setAccessible(true);
            targetField.setAccessible(true);
            try {
                if (!(sourceField.get(sourceBean) == null)) {
                    targetField.set(targetBean, sourceField.get(sourceBean));
                }
            } catch (IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return targetBean;
    }

    /*
     * @Description:手机验证码，正则表达式
     * @param Phone_number 手机号码
     * @return boolean 布尔值
     */
    public static boolean isPhone(String Phone_number) {
        String regex = "^((13[0-9])|(14(0|[5-7]|9))|(15([0-3]|[5-9]))|(16(2|[5-7]))|(17[0-8])|(18[0-9])|(19([0-3]|[5-9])))\\d{8}$";
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(Phone_number);
        return m.matches();
    }

}
